<?php

/**
 * +----------------------------------------------------------------------
 * | rayPHP framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2021-2031 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: miles <E-mail:80361700@qq.com> <QQ:80361700>
 * +----------------------------------------------------------------------
 * | Description: 框架核心
 * +----------------------------------------------------------------------
 */


namespace core;

class framework {
    public $clsMap = [];

    public function __construct() {
        // 开启session
        session_start();
        // 设置为本地时区
        date_default_timezone_set("prc");
        // 加载函数库公共文件
        include_once APP_PATH . "core/function/common.php";
        // composer自动加载
        include_once APP_PATH . "vendor/autoload.php";
        // 是否开启DEBUG模式
        if(APP_DEBUG) {
            $whoops = new \Whoops\Run;
            $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
            $whoops->register();
            ini_set("display_errors", "On");
            error_reporting(E_ALL & ~E_NOTICE);
        }
        else {
            ini_set("display_errors", "Off");
            error_reporting(0);
        }
    }

    public function run() {
        // 自动加载类
        spl_autoload_register([$this, 'load']);
        // 过滤所有非法参数
        new \core\libs\paramFilter();
        // 定义模块、风格模板
        new \core\libs\modular();
        // 加载日志类
        new \core\libs\log;
        // 初始化数据库链接
        \core\libs\db::getInstance()->connect();
        // 加载路由类
        new \core\libs\route();
    }

    public function load($cls){
        if(isset($this->clsMap[$cls])) {
            return true;
        }
        else {
            $file = APP_PATH . str_replace("\\", "/", $cls) . '.php';
            if(is_file($file)) {
                include_once $file;
                $this->clsMap[$cls] = $cls;
            }
            else {
                return false;
            }
        }
    }
}

